﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Build_List.ascx.cs"
    Inherits="ZLDC.Estate.UserControl.Build_List" %>
<!-- 根据楼盘，查询出下面的所有栋阁 -->

<script type="text/javascript">
    // 加载栋阁
    function loadBuild(bname) {
        var ename = $("#hEstateName").val();        // 楼盘名称
        var oldEname = $("#hOldEname").val();       // 之前输入的楼盘名称
        bname = bname.value; //栋阁名称

        var oldBname = $("#hOldBname").val();
        if (ename == oldEname && bname==oldBname)
            return;

        if ((typeof (ename) == "undefined") || ($("#iEstateName").val() == ""))
            ename = "";

        $.ajax({
            type: 'POST',
            url: '../Handler/HandlerIndex.ashx',
            data: 'act=buildlist_byeid&ename=' + escape(ename) + '&bname=' + escape(bname),
            timeout: '20000',
            error: function() {
                alert("加载栋阁失败！");
            }, success: function(json) {
                if (json != "") {
                    $("#hOldEname").val(ename);
                    $("#hOldBname").val(bname);
                    loadBuildCallBack(json);
                }
            }
        });
    }

    // 栋阁回调
    function loadBuildCallBack(response) {
        var json = eval("(" + response + ")");
        var record = json.record;

        var strDiv = "<input type='checkbox' onclick='QuResult1(this)' value='全部' title='全部' id='divcheck'/>全部</br>";
        if (record == null || record.length == 0) {
            document.getElementById("divBuildlist").innerHTML = strDiv;
            return;
        }

        // 显示下拉框
        document.getElementById("divBuildlist").style.display = "block";

        var buildname = "";                     // 栋阁名称
        var ename = $("#hEstateName").val();    // 楼盘名称  
        var recordLength = record.length;
        for (var i = 0; i < recordLength; i++) {
            buildname = record[i].finalname.replace(ename, "");
            if (buildname != ""){
                document.getElementById("hAllLevel").value += record[i].level + ",";
                strDiv += "<input type='checkbox' onclick='QuResult2(this,event, " + record[i].level + ")' value='" + record[i].id + "' title='" + buildname + "' id='divcheck'/>" + buildname + " " + record[i].othername + "</br>";
               }
        }
        document.getElementById("divBuildlist").innerHTML = strDiv;
    }

    Array.prototype.contains = function(element) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == element) {
                return true;
            }
        }
        return false;
    }

    // 全选
    function QuResult1(e) {
        var sdiv = document.getElementById("divBuildlist");
        var str = sdiv.getElementsByTagName("input");
        if (e.checked) {
            var result = "";
            var result2 = "";
            document.getElementById("iBuildList").value = document.getElementById("iBuildList").value.substring(0, document.getElementById("iBuildList").value.lastIndexOf(',') + 1);
            document.getElementById("hBuildList").value = document.getElementById("hBuildList").value.substring(0, document.getElementById("hBuildList").value.lastIndexOf(',') + 1);
            var sarray = document.getElementById("iBuildList").value.split(",");
            for (var i = 1; i < str.length; i++) {
                if (!sarray.contains(str[i].title)) {
                    str[i].checked = true;
                    result += str[i].title + ",";
                    result2 += str[i].value + ",";
                }
            }

            document.getElementById("iBuildList").value += result;
            document.getElementById("hBuildList").value += result2;
            document.getElementById("hBuildLevel").value = document.getElementById("hAllLevel").value;            
        }
        else {
            document.getElementById("iBuildList").value = "";
            document.getElementById("hBuildList").value = "";
            document.getElementById("hBuildLevel").value = "";
            for (var i = 1; i < str.length; i++) {
                str[i].checked = false;
            }
        }
    }

    // 单选
    function QuResult2(e, oEvent, level) {
        var arr = document.getElementById("iBuildList").value.split(",");

        // 限制最多选择10个
//        if (e.checked) {
//            var len = arr.length;
//            if (len > 10) {
//                e.checked = false;
//                alert("最多只能选择10个栋阁!");
//                return false;
//            }
//        }
        
        var sdiv = document.getElementById("divBuildlist");
        var str = sdiv.getElementsByTagName("input");
        document.getElementById("iBuildList").value = "," + document.getElementById("iBuildList").value.substring(0, document.getElementById("iBuildList").value.lastIndexOf(',') + 1);
        document.getElementById("hBuildList").value = "," + document.getElementById("hBuildList").value.substring(0, document.getElementById("hBuildList").value.lastIndexOf(',') + 1);
        document.getElementById("hBuildLevel").value = "," + document.getElementById("hBuildLevel").value.substring(0, document.getElementById("hBuildLevel").value.lastIndexOf(',') + 1);                       
        if (e.checked) {
            var sarray = document.getElementById("iBuildList").value.split(",");

            if (oEvent.shiftKey || oEvent.ctrlKey) {
                var start = Number(e.value) + 1;
                for (var i = e.value; i >= 0; i--) {
                    if (str[i].checked) {
                        start = i;
                        break;
                    }
                }
                for (var i = start; i <= Number(e.value) + 1; i++) {
                    str[i].checked = true;
                    if (!sarray.contains(str[i].title)) {
                        document.getElementById("iBuildList").value = document.getElementById("iBuildList").value + str[i].title + ",";
                        document.getElementById("hBuildList").value = document.getElementById("hBuildList").value + str[i].value + ",";
                        document.getElementById("hBuildLevel").value = document.getElementById("hBuildLevel").value + level + ",";
                    }
                }
            }
            else {
                if (!sarray.contains(e.title)) {
                    document.getElementById("iBuildList").value += e.title + ",";
                    document.getElementById("hBuildList").value += e.value + ",";
                    document.getElementById("hBuildLevel").value += level + ",";
                }
            }
        }
        else {
            if (oEvent.shiftKey || oEvent.ctrlKey) {
                var start = e.value + 1;
                for (var i = Number(e.value); i >= 0; i--) {
                    if (!str[i].checked) {
                        start = i;
                        break;
                    }
                }
                for (var i = start; i <= Number(e.value) + 1; i++) {
                    str[i].checked = false;
                    document.getElementById("iBuildList").value = document.getElementById("iBuildList").value.replace("," + str[i].title + ",", ",");
                    document.getElementById("hBuildList").value = document.getElementById("hBuildList").value.replace("," + str[i].value + ",", ",");
                    document.getElementById("hBuildLevel").value = document.getElementById("hBuildLevel").value.replace("," + level + ",", ",");
                }
            }
            else {
                var rreg = new RegExp("," + e.title + ",", "g");
                document.getElementById("iBuildList").value = document.getElementById("iBuildList").value.replace(rreg, ",");

                var rreg2 = new RegExp("," + e.value + ",", "g");
                document.getElementById("hBuildList").value = document.getElementById("hBuildList").value.replace(rreg2, ",");

                var rreg3 = new RegExp("," + level + ",", "g");
                document.getElementById("hBuildLevel").value = document.getElementById("hBuildLevel").value.replace(rreg3, ",");
            }
        }
        document.getElementById("iBuildList").value = document.getElementById("iBuildList").value.substr(1);
        document.getElementById("hBuildList").value = document.getElementById("hBuildList").value.substr(1);
        document.getElementById("hBuildLevel").value = document.getElementById("hBuildLevel").value.substr(1);

        closeDiv();
    }

    // 关闭层
    function closeDiv() {
        var name = document.activeElement.name;
        var html = document.getElementById("divBuildlist").innerHTML;

        if (html == "")
            document.getElementById("divBuildlist").style.display = "none";   
        else if (name == "iBuildList" || name == "") 
            document.getElementById("divBuildlist").style.display = "block";
        else
            document.getElementById("divBuildlist").style.display = "none";                     
    }        
    
</script>

<div id="con">
    <input id="iBuildList" name="iBuildList" onclick="loadBuild(this);" onkeyup="loadBuild(this);" style="width: 100px;" />
    <input id="hBuildList" name="hBuildList"  type="hidden" />
    <input id="hBuildLevel" name="hBuildLevel" type="hidden" />
    <input id="hAllLevel" name="hAllLevel"  type="hidden"/>
    <input id="hOldEname" name="hOldEname" type="hidden"/> 
    <input id="hOldBname" name="hOldBname"  type="hidden"/> 
       
</div>
<div id="divBuildlist" style="overflow: auto; z-index: 2; position: absolute; display: none;
    border: 1px; border-style: solid; background-color: #ffffff; height: 250px; width: 150px;
    text-align: left;">
</div>
